package project

import (
	"goskeleton/app/global/consts"
	"goskeleton/app/global/variable"
	"goskeleton/app/service/project/curd"
	"goskeleton/app/utils/response"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type Project struct {
}

func (e *Project) List(context *gin.Context) {
	page := context.GetFloat64(consts.ValidatorPrefix + "page")
	limit := context.GetFloat64(consts.ValidatorPrefix + "limit")
	projectName := context.GetString(consts.ValidatorPrefix + "project_name")
	responsibleEmployeeId := context.GetFloat64(consts.ValidatorPrefix + "responsible_employee_id")
	responsibleDepartmentId := context.GetFloat64(consts.ValidatorPrefix + "responsible_department_id")
	progress := context.GetFloat64(consts.ValidatorPrefix + "progress")
	expectedStartDateStart := context.GetString(consts.ValidatorPrefix + "expected_start_date_s")
	expectedStartDateEnd := context.GetString(consts.ValidatorPrefix + "expected_start_date_e")
	expectedEndDateStart := context.GetString(consts.ValidatorPrefix + "expected_end_date_s")
	expectedEndDateEnd := context.GetString(consts.ValidatorPrefix + "expected_end_date_e")
	variable.ZapLog.Info("expectedEndDateEnd", zap.Any("expectedEndDateEnd", expectedEndDateEnd), zap.Any("expectedEndDateStart", expectedEndDateStart))
	orderBy := "id desc"
	limitStart := (page - 1) * limit

	totalCounts, showList := curd.
		CreateProjectCurdFactory().
		ListData(
			int(limitStart),
			int(limit),
			orderBy,
			projectName,
			int(responsibleEmployeeId),
			int(responsibleDepartmentId),
			int(progress),
			expectedStartDateStart,
			expectedStartDateEnd,
			expectedEndDateStart,
			expectedEndDateEnd,
		)

	if totalCounts > 0 && showList != nil {
		response.Success(context, consts.CurdStatusOkMsg, gin.H{"count": totalCounts, "data": showList})
	} else {
		response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	}
}

func (e *Project) Detail(context *gin.Context) {
	projectId := context.GetFloat64(consts.ValidatorPrefix + "id")
	detail := curd.CreateProjectCurdFactory().ShowOneItem(int(projectId))

	if detail == nil {

		response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	} else {
		response.Success(context, consts.CurdStatusOkMsg, detail)
	}
}

func (e *Project) Create(context *gin.Context) {
	if curd.CreateProjectCurdFactory().InsertData(context) {
		response.Success(context, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg, "")
	}
}

func (e *Project) Edit(context *gin.Context) {
	if curd.CreateProjectCurdFactory().UpdateData(context) {
		response.Success(context, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(context, consts.CurdUpdateFailCode, consts.CurdUpdateFailMsg, "")
	}
}

func (e *Project) TotalStatistics(context *gin.Context) {
	totalStatusStatistics := curd.CreateProjectCurdFactory().GetTotalProgressStatistics()
	if totalStatusStatistics == nil {
		response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	} else {
		response.Success(context, consts.CurdStatusOkMsg, totalStatusStatistics)
	}
}

func (e *Project) GetAllProjectName(context *gin.Context) {
	projectName := curd.CreateProjectCurdFactory().GetAllProjectName()
	// if projectName == nil {
	// response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	// } else {
	response.Success(context, consts.CurdStatusOkMsg, projectName)
	// }
}

func (e *Project) Delete(context *gin.Context) {
	projectId := context.GetFloat64(consts.ValidatorPrefix + "id")
	if curd.CreateProjectCurdFactory().DeleteData(int(projectId)) {
		response.Success(context, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(context, consts.CurdDeleteFailCode, consts.CurdDeleteFailMsg, "")
	}
}